package gold.digger;


import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC101 {


    public boolean isSymmetric(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return true;
        }

        return isSymmetricRecur(root.left, root.right);
    }

    public boolean isSymmetricRecur(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }

        if (left == null || right == null) {
            return false;
        }

        if (left.val != right.val) {
            return false;
        }

        return isSymmetricRecur(left.left, right.right) && isSymmetricRecur(left.right, right.left);
    }


    public void inOrderTraverseLeftFirst(TreeNode root, StringBuilder res) {
        if (null == root) {
            res.append("#");
            return;
        }
        inOrderTraverseLeftFirst(root.left, res);
        res.append(String.valueOf(root.val));
        inOrderTraverseLeftFirst(root.right, res);
    }

    public void inOrderTraverseRightFirst(TreeNode root, StringBuilder res) {
        if (null == root) {
            res.append("#");
            return;
        }
        inOrderTraverseRightFirst(root.right, res);
        res.append(String.valueOf(root.val));
        inOrderTraverseRightFirst(root.left, res);
    }

    public void run() {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(2);
        TreeNode d = new TreeNode(3);
        TreeNode e = new TreeNode(4);
        TreeNode f = new TreeNode(4);
        TreeNode g = new TreeNode(3);

        a.left = b;
        a.right = c;

        b.left = d;
        b.right = e;

        c.left = f;
        c.right = g;
        System.out.println(isSymmetric(a));
    }


    public void run01() {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(2);
        TreeNode d = new TreeNode(2);
        TreeNode e = new TreeNode(2);
        TreeNode f = new TreeNode(4);
        TreeNode g = new TreeNode(3);

        a.left = b;
        a.right = c;

        b.left = d;
//        b.right = e;

        c.left = e;
//        c.right = g;
        System.out.println(isSymmetric(a));
    }

    public void run02() {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(2);
        TreeNode d = new TreeNode(2);
        TreeNode e = new TreeNode(2);
        TreeNode f = new TreeNode(4);
        TreeNode g = new TreeNode(3);

        a.right = b;

        System.out.println(isSymmetric(a));
    }

    public static void main(String[] args) {
        LC101 an = new LC101();
        an.run02();
    }
}
